package com.elsa.common.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.elsa.common.entity.ElsaResponse;
import com.elsa.common.exception.ElsaAuthException;

/**
 * @projectName：elsa-common
 * @typeName：BaseExceptionHandler
 * @author：cos
 * @date：2021年5月11日
 * @version
 * @desc：全局异常处理器:通用的异常类型捕获
 */
public class BaseExceptionHandler {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ElsaResponse handleException(Exception e) {
        log.error("系统内部异常，异常信息", e);
        return new ElsaResponse().message("系统内部异常");
    }

    @ExceptionHandler(value = ElsaAuthException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ElsaResponse handleFebsAuthException(ElsaAuthException e) {
        log.error("系统错误", e);
        return new ElsaResponse().message(e.getMessage());
    }
    
    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ElsaResponse handleAccessDeniedException(){
        return new ElsaResponse().message("没有权限访问该资源");
    }
}
